package com.fzd.code.captcha.service;

import com.fzd.code.captcha.service.i.ICaptchaManager;
import com.fzd.code.captcha.service.i.IRedisCaptchaService;
import com.fzd.code.captcha.service.i.ISessionCaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service("redis-mycatchap-service")
public class RedisMyCaptchaService implements IRedisCaptchaService {
    @Autowired
    private ICaptchaManager captchaManager;
    @Autowired
    private MyCaptchaUtil myCaptchaUtil;

    @Override
    public void create(String id, HttpServletResponse response) throws Exception {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        id += "-"+captchaManager.key;
        myCaptchaUtil.getRandcode(id, response);//输出图片方法
    }

    @Override
    public boolean validate(String id, String code) {
        id += "-"+captchaManager.key;
        return captchaManager.get(id)!=null && captchaManager.get(id).equalsIgnoreCase(code);
    }
}
